package ee.ut.maxorato_macobo.oop.mud.game;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Scanner;

import ee.ut.maxorato_macobo.oop.mud.network.NetSession;

public class PlayerPool {
	private static ArrayList<PlayerCharacter> playerList = new ArrayList<PlayerCharacter>();
	private static ArrayList<PlayerCharacter> disconnectList = new ArrayList<PlayerCharacter>();
	
	public static PlayerCharacter createPlayer(NetSession newSession, String newPlayerName) {
		PlayerCharacter newPlayer = new PlayerCharacter(newSession, newPlayerName);
		playerList.add(newPlayer);
		return newPlayer;
	}

	public static void removePlayer(PlayerCharacter player) {
		player.exitGame();
		playerList.remove(player);
	}
	
	public static ArrayList<PlayerCharacter> getPlayerList() {
		return playerList;
	}
	
	public static PlayerCharacter getPlayerByName(String findPlayerName) {
		ListIterator<PlayerCharacter> it = getPlayerList().listIterator();
		
		while(it.hasNext()) {
			PlayerCharacter findPlayer = it.next();
			
			if(findPlayer.getPlayerName().equals(findPlayerName)) {
				return findPlayer;
			}
		}
		
		return null;
	}
	
	public static void doCommandTick() {
		ListIterator<PlayerCharacter> it = getPlayerList().listIterator();
		
		while(it.hasNext()) {
			it.next().processCommandTick();
		}
	}
	
	public static void doFightTick() {
		ListIterator<PlayerCharacter> it = getPlayerList().listIterator();
		
		while(it.hasNext()) {
			it.next().processFightTick();
		}
	}
	
	public static void doOutputTick() {
		ListIterator<PlayerCharacter> it = getPlayerList().listIterator();
		
		while(it.hasNext()) {
			it.next().processOutputTick();
		}
	}
	
	public static void processDisconnection() {
		ListIterator<PlayerCharacter> it = disconnectList.listIterator();
		
		while(it.hasNext()) {
			it.next().kickPlayer();
		}
		
		disconnectList.clear();
	}
	
	public static ArrayList<PlayerCharacter> getDisconnectList() {
		return disconnectList;
	}
	
	public static void messageToAll(String message) {
		ListIterator<PlayerCharacter> it = getPlayerList().listIterator();
		
		while(it.hasNext()) {
			it.next().sendTickMessage(message);
		}
	}
	
	public static boolean existsGlobalPlayer(String playerName) {
		return new File("players/" + playerName + ".pwd").exists();
	}
	
	public static String getGlobalPlayerHash(String playerName) {
		try {
			Scanner sc = new Scanner(new File("players." + playerName + ".pwd"));
				
			return sc.nextLine();
		} catch(Exception e) {
			e.printStackTrace();
			
			return "";
		}
	}
	
	public static void saveGlobalPlayerHash(String playerName, String hash) {
		try {
			FileWriter sc = new FileWriter(new File("players." + playerName + ".pwd"));
			
			sc.write(hash);
			sc.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static boolean isValidName(String playerName) {
		if(playerName.length() < 2) return false;
		
		for(int i = 0; i < playerName.length(); i++) {
			if(!Character.isLetterOrDigit(playerName.charAt(i))) {
				return false;
			}
		}
		
		return true;
	}
	
	private PlayerPool() {
		
	}
}
